package com.zhiwei.fast.fragments

import android.os.Bundle
import androidx.appcompat.widget.AppCompatTextView
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.drake.brv.BindingAdapter
import com.drake.brv.utils.setup
import com.zhiwei.fast.R
import com.zhiwei.fast.data.MainItemModel
import com.zhiwei.fast.databinding.FragmentMainBinding
import com.zhiwei.fast.ext.wearLinear
import com.zhiwei.fast.fragments.models.MainFragmentViewModel
import com.zhiwei.most.base.fragment.BaseVmVbFragment
import com.zhiwei.most.ext.nav
import com.zhiwei.most.ext.navigateAction
import com.zhiwei.most.ext.views.setSwipePanel

/**
 * Create by zhiweizhu on 2022/2/25
 */
class MainFragment: BaseVmVbFragment<MainFragmentViewModel, FragmentMainBinding>() {

    private var mAdapter: BindingAdapter? = null

    override fun initView(savedInstanceState: Bundle?) {
        with(mViewBind){
            mAdapter = recyclerView.wearLinear().setup {
                addType<MainItemModel>(R.layout.layout_fragment_main_item)
                onBind {
                    val item = getModel<MainItemModel>()
                    findView<AppCompatTextView>(R.id.itemTitle).text =  item.title
                }
                onClick(R.id.itemTitle){
                    val item = getModel<MainItemModel>()
                    if(item.navId != 0){
                        nav().navigateAction(item.navId)
                    }else {
                        LogUtils.d("onClick: ${GsonUtils.toJson(item)}")
                    }
                }
            }

            swipePanel.setSwipePanel {
                requireActivity().onBackPressedDispatcher.onBackPressed()
            }
        }
    }

    override fun lazyLoadData() {
        mViewModel.loadData()
    }

    override fun createObserver() {
        with(mViewModel){
            getItemListLiveData().observe(viewLifecycleOwner){ list ->
                mAdapter?.let {
                    it.addModels(list)
                }
            }
        }
    }
}